package com.yanxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yanxi.mapper.ClazzMapper;
import com.yanxi.pojo.Clazz;
import com.yanxi.pojo.EmpQueryParam;
import com.yanxi.pojo.PageResult;
import com.yanxi.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public PageResult<Clazz> pageClazz(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Clazz> clazzList = clazzMapper.pageClazz(empQueryParam);
        clazzList.forEach(clazz->{
            if (clazz.getBeginDate().isAfter(LocalDate.now())){
                clazz.setStatus("未开始");
            }else if (clazz.getEndDate().isBefore(LocalDate.now())){
                clazz.setStatus("已结束");
            }else {
                clazz.setStatus("进行中");
            }
        });
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }

    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.save(clazz);
    }

    @Override
    public Clazz findById(Integer id) {
        return clazzMapper.findById(id);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> listAll() {
        return clazzMapper.listAll();
    }
}
